package edu.szte.ovrt.datahandler.model;

import java.util.LinkedList;
import java.util.List;

public class Section extends ModelBase {
	private final Document m_parent;
	private String m_name = "";
	private final List<Item> m_items = new LinkedList<Item>();

	public Section(final Document parent) {
		m_parent = parent;
	}

	public Section(final Document parent, final String name) {
		this(parent);
		setName(name);
	}

	@Override
	public String getName() {
		return m_name;
	}

	public void setName(final String name) {
		m_name = name;
	}

	public List<Item> getItems() {
		return m_items;
	}

	public void addItem(final Item item) {
		m_items.add(item);
	}

	public Document getParent() {
		return m_parent;
	}

	public Item newItem(final String key) {
		return newItem(key, "");
	}

	public Item newItem(final String key, final String value) {
		final Item item = new Item(this, key, value);
		addItem(item);
		return item;
	}

	@Override
	public boolean equals(final Object obj) {
		if (obj == null || !(obj instanceof Section)) { return false; }
		final Section other = (Section) obj;
		return getName().equals(other.getName()) && m_items.equals(other.m_items);
	}

	@Override
	public int hashCode() {
		int hashCode = getName().hashCode();
		for (final Item item : m_items) {
			hashCode ^= item.hashCode();
		}
		return hashCode;
	}

	@Override
	public String toString() {
		return String.format("Section ({0}): {1}", getItems().size(), getName());
	}
}
